<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发布新帖 - 环保社区</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }

        .content {
            padding: 30px;
        }

        .back-btn {
            display: inline-block;
            background: #6c757d;
            color: white;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 5px;
            margin-bottom: 30px;
            transition: all 0.3s;
        }

        .back-btn:hover {
            background: #5a6268;
            text-decoration: none;
            color: white;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-label {
            display: block;
            font-weight: bold;
            margin-bottom: 8px;
            color: #333;
            font-size: 16px;
        }

        .form-input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s;
            background: #fff;
        }

        .form-input:focus {
            outline: none;
            border-color: #4ECDC4;
            box-shadow: 0 0 10px rgba(78, 205, 196, 0.2);
        }

        .form-select {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 16px;
            background: white;
            cursor: pointer;
            transition: all 0.3s;
        }

        .form-select:focus {
            outline: none;
            border-color: #4ECDC4;
            box-shadow: 0 0 10px rgba(78, 205, 196, 0.2);
        }

        .form-textarea {
            width: 100%;
            min-height: 200px;
            padding: 15px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 16px;
            font-family: inherit;
            resize: vertical;
            transition: all 0.3s;
        }

        .form-textarea:focus {
            outline: none;
            border-color: #4ECDC4;
            box-shadow: 0 0 10px rgba(78, 205, 196, 0.2);
        }

        .form-help {
            font-size: 14px;
            color: #666;
            margin-top: 5px;
        }

        .form-actions {
            display: flex;
            gap: 15px;
            justify-content: flex-end;
            margin-top: 30px;
        }

        .btn {
            padding: 12px 25px;
            border: none;
            border-radius: 25px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }

        .btn-primary {
            background: linear-gradient(135deg, #4ECDC4 0%, #44A08D 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(78, 205, 196, 0.3);
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6268;
            text-decoration: none;
            color: white;
        }

        .preview-section {
            margin-top: 30px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 10px;
            border: 1px solid #e9ecef;
        }

        .preview-title {
            font-size: 1.2em;
            font-weight: bold;
            margin-bottom: 15px;
            color: #333;
        }

        .preview-content {
            background: white;
            padding: 20px;
            border-radius: 8px;
            border: 1px solid #dee2e6;
        }

        .preview-post-title {
            font-size: 1.4em;
            font-weight: bold;
            color: #333;
            margin-bottom: 10px;
        }

        .preview-post-category {
            display: inline-block;
            background: #4ECDC4;
            color: white;
            padding: 4px 12px;
            border-radius: 15px;
            font-size: 12px;
            margin-bottom: 15px;
        }

        .preview-post-content {
            color: #666;
            line-height: 1.6;
            white-space: pre-wrap;
        }

        .error-message {
            background: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            border: 1px solid #f5c6cb;
        }

        .success-message {
            background: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            border: 1px solid #c3e6cb;
        }

        .char-counter {
            text-align: right;
            font-size: 14px;
            color: #666;
            margin-top: 5px;
        }

        .char-counter.warning {
            color: #e74c3c;
        }

        @media (max-width: 768px) {
            .form-actions {
                flex-direction: column;
            }
            
            .btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>发布新帖</h1>
            <p>分享你的环保心得和垃圾分类技巧</p>
        </div>

        <div class="content">
            <a href="{{ url_for('forum') }}" class="back-btn">← 返回社区</a>
            
            {% with messages = get_flashed_messages(with_categories=true) %}
                {% if messages %}
                    {% for category, message in messages %}
                        <div class="{% if category == 'success' %}success-message{% else %}error-message{% endif %}">
                            {{ message }}
                        </div>
                    {% endfor %}
                {% endif %}
            {% endwith %}

            <form method="POST" id="postForm">
                <div class="form-group">
                    <label for="title" class="form-label">帖子标题 *</label>
                    <input type="text" id="title" name="title" class="form-input" 
                           placeholder="请输入帖子标题..." required maxlength="100"
                           value="{{ request.form.title or '' }}">
                    <div class="char-counter" id="titleCounter">0/100</div>
                </div>

                <div class="form-group">
                    <label for="category" class="form-label">帖子分类 *</label>
                    <select id="category" name="category" class="form-select" required>
                        <option value="">请选择分类</option>
                        <option value="tips" {{ 'selected' if request.form.category == 'tips' }}>环保心得</option>
                        <option value="question" {{ 'selected' if request.form.category == 'question' }}>问题求助</option>
                        <option value="share" {{ 'selected' if request.form.category == 'share' }}>经验分享</option>
                        <option value="discussion" {{ 'selected' if request.form.category == 'discussion' }}>讨论交流</option>
                    </select>
                </div>

                <div class="form-group">
                    <label for="content" class="form-label">帖子内容 *</label>
                    <textarea id="content" name="content" class="form-textarea" 
                              placeholder="请输入帖子内容..." required maxlength="2000">{{ request.form.content or '' }}</textarea>
                    <div class="char-counter" id="contentCounter">0/2000</div>
                    <div class="form-help">支持换行，最多2000字符</div>
                </div>

                <div class="form-actions">
                    <button type="button" class="btn btn-secondary" onclick="previewPost()">预览</button>
                    <button type="submit" class="btn btn-primary">发布帖子</button>
                </div>
            </form>

            <div id="previewSection" class="preview-section" style="display: none;">
                <div class="preview-title">帖子预览</div>
                <div class="preview-content">
                    <div class="preview-post-category" id="previewCategory"></div>
                    <div class="preview-post-title" id="previewTitle"></div>
                    <div class="preview-post-content" id="previewContent"></div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 字符计数器
        function updateCharCounter(inputId, counterId, maxLength) {
            const input = document.getElementById(inputId);
            const counter = document.getElementById(counterId);
            const currentLength = input.value.length;
            
            counter.textContent = `${currentLength}/${maxLength}`;
            
            if (currentLength > maxLength * 0.9) {
                counter.classList.add('warning');
            } else {
                counter.classList.remove('warning');
            }
        }

        // 绑定字符计数器事件
        document.getElementById('title').addEventListener('input', function() {
            updateCharCounter('title', 'titleCounter', 100);
        });

        document.getElementById('content').addEventListener('input', function() {
            updateCharCounter('content', 'contentCounter', 2000);
        });

        // 预览功能
        function previewPost() {
            const title = document.getElementById('title').value;
            const category = document.getElementById('category').value;
            const content = document.getElementById('content').value;
            
            if (!title || !category || !content) {
                alert('请填写完整的帖子信息后再预览');
                return;
            }
            
            // 分类名称映射
            const categoryNames = {
                'tips': '环保心得',
                'question': '问题求助',
                'share': '经验分享',
                'discussion': '讨论交流'
            };
            
            document.getElementById('previewCategory').textContent = categoryNames[category] || category;
            document.getElementById('previewTitle').textContent = title;
            document.getElementById('previewContent').textContent = content;
            
            document.getElementById('previewSection').style.display = 'block';
            document.getElementById('previewSection').scrollIntoView({ behavior: 'smooth' });
        }

        // 表单验证
        document.getElementById('postForm').addEventListener('submit', function(e) {
            const title = document.getElementById('title').value.trim();
            const category = document.getElementById('category').value;
            const content = document.getElementById('content').value.trim();
            
            if (!title) {
                alert('请输入帖子标题');
                e.preventDefault();
                return;
            }
            
            if (!category) {
                alert('请选择帖子分类');
                e.preventDefault();
                return;
            }
            
            if (!content) {
                alert('请输入帖子内容');
                e.preventDefault();
                return;
            }
            
            if (title.length > 100) {
                alert('标题长度不能超过100字符');
                e.preventDefault();
                return;
            }
            
            if (content.length > 2000) {
                alert('内容长度不能超过2000字符');
                e.preventDefault();
                return;
            }
        });

        // 页面加载时初始化计数器
        window.onload = function() {
            updateCharCounter('title', 'titleCounter', 100);
            updateCharCounter('content', 'contentCounter', 2000);
        };

        // 自动保存草稿（可选功能）
        function saveDraft() {
            const title = document.getElementById('title').value;
            const category = document.getElementById('category').value;
            const content = document.getElementById('content').value;
            
            localStorage.setItem('forum_draft', JSON.stringify({
                title: title,
                category: category,
                content: content,
                timestamp: Date.now()
            }));
        }

        // 加载草稿
        function loadDraft() {
            const draft = localStorage.getItem('forum_draft');
            if (draft) {
                const draftData = JSON.parse(draft);
                // 只加载24小时内的草稿
                if (Date.now() - draftData.timestamp < 24 * 60 * 60 * 1000) {
                    if (confirm('检测到未发布的草稿，是否恢复？')) {
                        document.getElementById('title').value = draftData.title || '';
                        document.getElementById('category').value = draftData.category || '';
                        document.getElementById('content').value = draftData.content || '';
                        updateCharCounter('title', 'titleCounter', 100);
                        updateCharCounter('content', 'contentCounter', 2000);
                    }
                }
            }
        }

        // 定期保存草稿
        setInterval(saveDraft, 30000); // 每30秒保存一次

        // 页面加载时尝试恢复草稿
        document.addEventListener('DOMContentLoaded', loadDraft);

        // 发布成功后清除草稿
        document.getElementById('postForm').addEventListener('submit', function() {
            localStorage.removeItem('forum_draft');
        });
    </script>
</body>
</html>